using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using cframework_xlua;
using UnityEngine.SceneManagement;

public static class StartHotUpdate
{
    static HotUpdateCodeInfo info = null;
    public static LoadDllConfig loadDllConfig;

    public static int GetRunningHotUpdateCodeVersion()
    {
        if(info == null)
        {
            return -1;
        }
        return info.hotupdate_version;
    }


    public static IEnumerator CallEnterGameAsync()
    {
        Debug.Log("CallEnterGame");

        if(loadDllConfig == null)
        {
            yield return false;
        }

        var ret = CommonUtil.CallMethodWithResult(loadDllConfig.startGameDllName.Replace(".dll", ""), loadDllConfig.startGameClassName, loadDllConfig.startGameMethodName, new object[] { loadDllConfig.gameScene });
        var task = ((IEnumerator)ret.Item2).WithResult();
        yield return task;
        var result = task.Current;

        yield return result;
    }





    public static IEnumerator StartAsync(AssetBundle ab, string loadDllInfoStr)
    {
        yield return null;

        var loadDllConfig = Newtonsoft.Json.JsonConvert.DeserializeObject<LoadDllConfig>(loadDllInfoStr);

        if (ab != null)
        {
            info = HotUpdateDllMgr.LoadVersionInfoConfigFromBundle(ab);

            GameObject prefab = ab.LoadAsset(loadDllConfig.hotUpdatePrefab) as GameObject;
            GameObject.Instantiate(prefab);


            yield return true;
        }
        else
        {
#if UNITY_EDITOR
            if (Application.isEditor)
            {
                var prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(loadDllConfig.hotUpdatePrefab);
                GameObject.Instantiate(prefab);

                yield return true;
            }
            else
#endif
            {
                yield return false;
            }
        }
    }

}